package com.meilian.collect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meilian.clients.ProductClient;
import com.meilian.collect.mapper.CollectMapper;
import com.meilian.collect.service.CollectService;
import com.meilian.param.ProductCollectParam;
import com.meilian.pojo.Collect;
import com.meilian.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//收藏实现类
@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private ProductClient productClient;
    /**
     * 添加收藏的方法
     * @param collect
     * @return 001 004
     */
    @Override
    public R save(Collect collect) {
        //1.先查询是否存在
        QueryWrapper<Collect> collectQueryWrapper=new QueryWrapper<>();
        collectQueryWrapper.eq("user_id",collect.getUserId());
        collectQueryWrapper.eq("product_id",collect.getProductId());

        Long count = collectMapper.selectCount(collectQueryWrapper);
        if (count>0) {
            return R.fail("收藏已添加,无需再次添加!");
        }

        //2.不存在就添加

        collect.setCollectTime(System.currentTimeMillis());
        int rows = collectMapper.insert(collect);

        log.info( "CollectServiceImpl.save业务结束，结果:{}",rows);

        return R.ok("收藏添加成功!");
    }

    /**
     * 按用户id查询收藏商品列表
     * @param userId
     * @return
     */
    @Override
    public R list(Integer userId) {
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.select("product_id");

        //根据用户id查收藏的商品id
        List<Object> idsObject = collectMapper.selectObjs(queryWrapper);
        ProductCollectParam productCollectParam=new ProductCollectParam();

        List<Integer> ids=new ArrayList<>();
        for (Object o :idsObject) {
            ids.add((Integer)o);
        }
        //把用户收藏的商品id放到ProductCollectParam中
        productCollectParam.setProductIds(ids);

        R r = productClient.productIds(productCollectParam);
        log.info( "CollectServiceImpl.list业务结束，结果:{}",r);

        return r;
    }

    /**
     * 按用户id和商品id移除收藏的商品
     * @param collect 包含user_id和product_id
     * @return
     */
    @Override
    public R remove(Collect collect) {
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",collect.getUserId());
        queryWrapper.eq("product_id",collect.getProductId());

        int rows = collectMapper.delete(queryWrapper);
        log.info( "CollectServiceImpl.remove业务结束，结果:{}",rows);
        return R.ok("您收藏的商品移除成功!");
    }

    @Override
    public R removeByPid(Integer productId) {
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("product_id",productId);

        int rows = collectMapper.delete(queryWrapper);
        log.info( "CollectServiceImpl.removeByPid业务结束，结果:{}",rows);
        return R.ok("收藏商品删除成功!");
    }
}
